﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_UserControl_danh_muc_edit : System.Web.UI.UserControl
{
    protected int iDM_ID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        iDM_ID = OsPortal.oStringHelper.fncCnvNullToInt(clsCommon.getParameter(basConst.QR_URL_ID));
        if (!IsPostBack)
        {
            if (iDM_ID != 0)
            {
                xGetDetails();
            }
            xGetChuyenMucCha();
        }
    }

    /// <summary>
    /// Sự kiện click
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        var strError = "";
        if (iDM_ID != 0)
        {
            if (!clsDanhMuc.DANHMUC_ACT(basConst.MOD_UPDATE, xGetValueForm(), out strError))
            {
                OsPortal.oHtmlHelper.AlertAndRederect(strError, "danh-muc-danh-sach", this.Page);
            }
            OsPortal.oStringHelper.ClearCache();
            OsPortal.oHtmlHelper.AlertAndRederect(basConst.SUSSCESS_ACT, "?mo=danh-muc-danh-sach", this.Page);
        }
        else
        {
            if (!clsDanhMuc.DANHMUC_ACT(basConst.MOD_ADD, xGetValueForm(), out strError))
            {
                OsPortal.oHtmlHelper.AlertAndRederect(strError, "?mo=danh-muc-danh-sach", this.Page);
            }
        }
    }

    protected void xGetDetails() {
        DataTable dt = clsDanhMuc.fncGetByID(iDM_ID);
        txtColor.Text = dt.Rows[0]["dm_color"].ToString();
        txtMoTa.Text = dt.Rows[0]["dm_mota"].ToString();
        txtTieuDe.Text = dt.Rows[0]["dm_tieude"].ToString();
        hdAnhDanhMuc.Value = dt.Rows[0]["dm_anh"].ToString();
        hdLogo.Value = dt.Rows[0]["dm_logo"].ToString();
        ddlChuyenMuc.SelectedValue = dt.Rows[0]["DM_ID"].ToString();
        txtNoiDung.Text = System.Net.WebUtility.HtmlDecode(dt.Rows[0]["dm_noidung"].ToString());
        string status = dt.Rows[0]["dm_status"].ToString();
        hdPrentID.Value = dt.Rows[0]["dm_parent_id"].ToString();
        if (status == "True") { rbtShow.Checked = true; } else { rbtHide.Checked = true; }
    }

    /// <summary>
    /// Lấy chuyên mục cha
    /// </summary>
    protected void xGetChuyenMucCha()
    {
        ddlChuyenMuc.DataSource = clsDanhMuc.fncGetAll();
        ddlChuyenMuc.DataTextField = "dm_tieude";
        ddlChuyenMuc.DataValueField = "DM_ID";
        ddlChuyenMuc.DataBind();
    }

    /// <summary>
    /// Lấy giá trị từ form
    /// </summary>
    /// <returns></returns>
    private clsDanhMuc.stDanhMuc xGetValueForm()
    {
        clsDanhMuc.stDanhMuc objDanhMuc = new clsDanhMuc.stDanhMuc();
        objDanhMuc.iDM_ID = OsPortal.oStringHelper.fncCnvNullToInt(clsCommon.getParameter(basConst.QR_URL_ID));
        if (iDM_ID != 0)
        {
            objDanhMuc.iDM_PARENT_ID = OsPortal.oStringHelper.fncCnvNullToInt(hdPrentID.Value);
        }
        else {
            objDanhMuc.iDM_PARENT_ID = OsPortal.oStringHelper.fncCnvNullToInt(ddlChuyenMuc.SelectedValue);
        }
        objDanhMuc.strDM_TIEUDE = txtTieuDe.Text.Trim();
        objDanhMuc.strDM_MOTA = txtMoTa.Text.Trim();
        objDanhMuc.strDM_NOIDUNG = Server.HtmlDecode(txtNoiDung.Text);
        objDanhMuc.strDM_COLOR = txtColor.Text.Trim();
        objDanhMuc.strDM_NGAYTAO = DateTime.Now;
        objDanhMuc.strDM_NGAYUPDATE = DateTime.Now;
        //
        int iDisplay = 1;
        if (rbtHide.Checked) iDisplay = 0;
        objDanhMuc.iDM_STATUS = iDisplay;
        //
        //Ảnh
        string strAnh = hdAnhDanhMuc.Value;
        if (fulAnhDanhMuc.HasFile)
        {
            string strOutImagesName;
            OsPortal.oFileHelper.UploadAnhResizeImage(fulAnhDanhMuc, basConst.PATH_IMAGES_DANHMUC, 500, out strOutImagesName, "", false);
            if (!string.IsNullOrEmpty(strOutImagesName))
                strAnh = basConst.PATH_IMAGES_DANHMUC.Replace("~/", "") + strOutImagesName;

        }
        objDanhMuc.strDM_ANH = strAnh;

        //Ảnh LoGo
        string strLogo = hdLogo.Value;
        if (fulLogo.HasFile)
        {
            string strOutImagesName;
            OsPortal.oFileHelper.UploadAnhResizeImage(fulLogo, basConst.PATH_IMAGES_DANHMUC, 500, out strOutImagesName, "", false);
            if (!string.IsNullOrEmpty(strOutImagesName))
                strLogo = basConst.PATH_IMAGES_DANHMUC.Replace("~/", "") + strOutImagesName;

        }
        objDanhMuc.strDM_LOGO = strLogo;

        return objDanhMuc;
    }

}